#!/bin/bash

# init
kubeadm config images pull --config kubeadm-config.yaml
kubeadm init --config kubeadm-config.yaml --ignore-preflight-errors NumCPU

# configure local kubectl
cp /etc/kubernetes/admin.conf ~/.kube/config

# get status
kubectl get ns
kubectl -n kube-system get pods

# master can be scheduled
kubectl taint node fkxxyz-tc node-role.kubernetes.io/master-



# install fannel service
docker pull quay.io/coreos/flannel:v0.13.0
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.13.0/Documentation/kube-flannel.yml
kubectl -n kube-system get daemonset

# install nginx-ingress
(
  # clone git repo
  git clone https://github.com/nginxinc/kubernetes-ingress.git
  cd kubernetes-ingress
  git checkout v1.10.0
  cd deployments
  
  # create namespace and serviceaccount
  kubectl apply -f common/ns-and-sa.yaml
  kubectl get ns nginx-ingress
  kubectl -n nginx-ingress get sa nginx-ingress
  
  # Configure RBAC
  kubectl apply -f rbac/rbac.yaml
  kubectl get ClusterRole nginx-ingress
  kubectl -n nginx-ingress get ClusterRoleBinding nginx-ingress
  
  # RBAC App Protect only
  kubectl apply -f rbac/ap-rbac.yaml
  kubectl get ClusterRole nginx-ingress-app-protect
  kubectl -n nginx-ingress get ClusterRoleBinding nginx-ingress-app-protect
  
  # Create a secret
  kubectl apply -f common/default-server-secret.yaml
  kubectl -n nginx-ingress get secret default-server-secret
  
  # nginx-ingress configMap
  kubectl apply -f common/nginx-config.yaml
  kubectl -n nginx-ingress get cm nginx-config
  
  # Create an IngressClass resource as default Ingress controller
  cat common/ingress-class.yaml | sed 's/# //g' | kubectl apply -f /dev/stdin
  kubectl get IngressClass
  
  # Create Custom Resources
  kubectl apply -f common/crds/k8s.nginx.org_virtualservers.yaml
  kubectl apply -f common/crds/k8s.nginx.org_virtualserverroutes.yaml
  kubectl apply -f common/crds/k8s.nginx.org_transportservers.yaml
  kubectl apply -f common/crds/k8s.nginx.org_policies.yaml
  
  # Deploy the Ingress Controller
  kubectl apply -f daemon-set/nginx-ingress.yaml
  kubectl -n nginx-ingress get ds
  kubectl -n nginx-ingress get pods
)

# install Dashboard v2
kubectl apply -f https://kuboard.cn/install-script/kuboard.yaml
kubectl apply -f kuboard_v2_ingress.yaml
kubectl -n kube-system get pods
kubectl -n kube-system get deployments
kubectl -n kube-system get svc
kubectl -n kube-system describe ingress
curl https://k8s2.fkxxyz.com/

kubectl create secret tls fkxxyz-secret --cert=$HOME/.config/fkxxyz.com/fullchain1.pem --key=$HOME/.config/fkxxyz.com/privkey1.pem

# install cert-manager
kubectl apply -f https://github.com/jetstack/cert-manager/releases/download/v1.2.0/cert-manager.yaml


# install Dashboard service
docker pull kubernetesui/dashboard:v2.0.0
docker pull kubernetesui/metrics-scraper:v1.0.4
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml
kubectl -n kubernetes-dashboard get deployments
kubectl -n kubernetes-dashboard get svc



